package com.zs.grain.service


import android.content.Context
import android.media.AudioManager
import android.media.MediaPlayer
import android.media.MediaPlayer.OnCompletionListener
import android.media.SoundPool
import android.net.Uri
//import com.jeremyliao.liveeventbus.utils.AppUtils.getApplicationContext
import com.zs.grain.R
import com.zs.grain.tool.clockManage.LocalDataBase
import javax.inject.Inject
import javax.inject.Singleton


/**
 * Created by Wesley on 2017/7/15.
 */

@Singleton
class SoundService @Inject constructor(
    val mContext: Context,
    val mDataService: DataService
) {
    lateinit var mSoundPool: SoundPool
    lateinit var mSoundMap: MutableMap<EnumSound, Int>;
    var lastSoundIndex = 0;

    lateinit var mMediaPlayer: MediaPlayer
    lateinit var mAudioManager: AudioManager
    //var lastSoundIndex2 = 0;
    fun init2(){
        mMediaPlayer = MediaPlayer()
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION)
        mAudioManager = mContext.getSystemService(Context.AUDIO_SERVICE) as AudioManager
    }

    fun init() {
        mSoundPool = SoundPool.Builder().setMaxStreams(16).build();
        mSoundMap = mutableMapOf();
        mSoundMap.set(EnumSound.DINGDONG, mSoundPool.load(mContext, R.raw.dingdong, 1))
        mSoundMap.set(EnumSound.DOSAGE, mSoundPool.load(mContext, R.raw.dosage, 1))
        mSoundMap.set(EnumSound.MAKING_0, mSoundPool.load(mContext, R.raw.making_0, 1))
        mSoundMap.set(EnumSound.MAKING_1, mSoundPool.load(mContext, R.raw.making_1, 1))
        mSoundMap.set(EnumSound.MAKING_2, mSoundPool.load(mContext, R.raw.making_2, 1))
        mSoundMap.set(EnumSound.MAKING_3, mSoundPool.load(mContext, R.raw.making_3, 1))
        mSoundMap.set(EnumSound.MAKING_4, mSoundPool.load(mContext, R.raw.making_4, 1))
        mSoundMap.set(EnumSound.MAKING_5, mSoundPool.load(mContext, R.raw.making_5, 1))
        mSoundMap.set(EnumSound.TAKE, mSoundPool.load(mContext, R.raw.take_2, 1))
        mSoundMap.set(EnumSound.FINISH, mSoundPool.load(mContext, R.raw.finish, 1))
        mSoundMap.set(EnumSound.SCAN, mSoundPool.load(mContext, R.raw.scan, 1))
    }
    fun playSound2(index:Int){
//        mMediaPlayer?.reset()

//        mAudioManager?.setStreamVolume(
//            AudioManager.STREAM_MUSIC,
//            mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC),//.getStreamMaxVolume(AudioManager.STREAM_MUSIC) - 12,
//            AudioManager.FLAG_PLAY_SOUND
//        )
//        mAudioManager?.setStreamVolume(
//            AudioManager.STREAM_NOTIFICATION,
//            mAudioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION),//.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION),
//            AudioManager.FLAG_PLAY_SOUND
//        )
//        when(index){
//            1->mMediaPlayer?.setDataSource(getApp(), Uri.parse("android.resource://com.zs.grain/" + R.raw.dingdong));
//            2->mMediaPlayer?.setDataSource(getApp(), Uri.parse("android.resource://com.zs.grain/" + R.raw.dosage));
//            3->mMediaPlayer?.setDataSource(getApp(), Uri.parse("android.resource://com.zs.grain/" + R.raw.scan));
//            4->mMediaPlayer?.setDataSource(getApp(), Uri.parse("android.resource://com.zs.grain/" + R.raw.making));
//            5->mMediaPlayer?.setDataSource(getApp(), Uri.parse("android.resource://com.zs.grain/" + R.raw.take_2));
//            6->mMediaPlayer?.setDataSource(getApp(), Uri.parse("android.resource://com.zs.grain/" + R.raw.finish));
//        }

//        mMediaPlayer?.prepare()
//        mMediaPlayer.setOnCompletionListener(OnCompletionListener {
//
//            mAudioManager?.setStreamVolume(
//                AudioManager.STREAM_MUSIC,
//                mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC),
//                AudioManager.FLAG_PLAY_SOUND
//            )
//        })
//
//        mMediaPlayer?.start()
    }

    fun playSound(streamType:Int,index: EnumSound) {
        mSoundPool?.stop(lastSoundIndex)
        val am = mContext.getSystemService(Context.AUDIO_SERVICE) as AudioManager
        val audioMaxVolumn = am.getStreamMaxVolume(streamType).toFloat()
        val volumnCurrent = am.getStreamVolume(streamType).toFloat()
        val volumnRatio = volumnCurrent / audioMaxVolumn
        lastSoundIndex =
            mSoundPool?.play(mSoundMap.get(index)!!, volumnRatio, volumnRatio, 1, 0, 1f)
    }

    fun stopSound() {
        mSoundPool?.stop(lastSoundIndex)
    }

    fun stopSound2(){
        mAudioManager?.setStreamVolume(
            AudioManager.STREAM_MUSIC,
            mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC),
            AudioManager.FLAG_PLAY_SOUND
        )
        mMediaPlayer?.stop()
    }

    fun soundAdd(vol:Int,streamType:Int){
        var maxVol = -1
        if(streamType==3) maxVol = 16
        else maxVol = 7
        val am = mContext.getSystemService(Context.AUDIO_SERVICE) as AudioManager
        var volumnCurrent = am.getStreamVolume(streamType)
        if(volumnCurrent<maxVol) {
            am.setStreamVolume(
                streamType,//AudioManager.STREAM_MUSIC,
                volumnCurrent + vol,
                AudioManager.FLAG_PLAY_SOUND
            )
        }
    }

    fun soundDel(vol:Int,streamType:Int){
        var minVol = 0
        if(streamType==5) minVol = 1
        val am = mContext.getSystemService(Context.AUDIO_SERVICE) as AudioManager
        var volumnCurrent = am.getStreamVolume(streamType)
        if(volumnCurrent>minVol) {
            am.setStreamVolume(
                streamType,
                volumnCurrent - vol,
                AudioManager.FLAG_PLAY_SOUND
            )
        }
    }

    companion object {
    }

    enum class EnumSound {
        DINGDONG,
        DOSAGE,
        SCAN,
        MAKING_0,
        MAKING_1,
        MAKING_2,
        MAKING_3,
        MAKING_4,
        MAKING_5,
        TAKE,
        FINISH
    }
}

